<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * @author duongbq
 * Date 2011 - 05 -11
 */
class Invoice_Model extends CI_Model
{
    public function __construct() {
        parent::__construct();
    }
    
    private function _set_where($param = array()) {

        if (isset($param['id']))
            $this->db->where('invoice.id', $param['id']);
        
        if (isset($param['account_id']))
            $this->db->where('invoice.account_id', $param['account_id']);
        
        if (isset($param['year']) && $param['year'] != -1)
            $this->db->where('year(invoice_date)', $param['year']);

        if (isset($param['month']) && $param['month'] != -1)
            $this->db->where('month(invoice_date)', $param['month']);
        
        if (isset($param['day']) && $param['day'] != -1)
            $this->db->where('day(invoice_date)', $param['day']);
        
    }
    
    function insert_invoice($param = array()){
        if(!empty($param)){
            if($this->db->insert('invoice',$param)) return $this->db->insert_id();
        }
        return false;
    }

    function get_invoice($param = array()) {

        $this->_set_where($param);

        if (isset($param['order_by']))
            $this->db->order_by($param['order_by']);
        else
            $this->db->order_by('invoice_date');

        if (isset($param['limit']) && isset($param['offset']))
            $this->db->limit($param['limit'], $param['offset']);

        return $this->db->get('invoice')->result();
    }

    function get_invoice_count($param = array()) {
        return count($this->get_invoice($param));
    }
    
    function delete_invoice($param = array()) {
        
        if (isset($param['id'])){

            $this->db->where('id', $param['id']);
            return $this->db->delete('invoice');
        }
    }
}
